#!/bin/bash
#(c) Copyright 2013 Barry Kauler bkhome.org
#2013 Licence GPL3 (/usr/share/doc/legal).
#this is startup code, called from /sbin/pup_event_frontend_d
# (which in turn is called from /root/.xinitrd when X starts).

#exec &>/tmp/${0##*/}.log ; set -x #debug

rm -f /tmp/services/x_display

sleep 3 #1 #let the dust settle after X has started. 120718 reduce 2 to 1. 121105 try 2, see also /usr/sbin/delayedrun. 121212
#w471 slow cpus need more delay (well, rox does)...
DELAYFACTOR=0
CPUMHZ=`grep -m 1 -i '^cpu MHz' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'` #my laptop: 933.000
if [ $CPUMHZ ];then
 [ $CPUMHZ -gt 100 ] && DELAYFACTOR=$((1600 / $CPUMHZ)) #120829 L18L: need 1 sec for my 1514 MHZ CPU.  #`expr 1100 \/ $CPUMHZ`
else #120718 raspi: 697.95  my laptop: 4789.47
 BOGOMIPS=`grep -m 1 -i '^bogomips' /proc/cpuinfo | tr -d ' ' | cut -f 2 -d ':' | cut -f 1 -d '.'`
 [ $BOGOMIPS ] && [ $BOGOMIPS -gt 200 ] && DELAYFACTOR=`expr 2100 \/ $BOGOMIPS`
fi
[ $DELAYFACTOR -gt 0 ] && sleep $DELAYFACTOR

pidof X &>/dev/null || pidof Xorg &>/dev/null || exit 3 #X not running.

#-------------------------------------
echo "DISPLAY='${DISPLAY}'" > /tmp/services/x_display
[ -f /tmp/services/user_info ] && . /tmp/services/user_info
if [ "$USER" != "root" ] ; then
	export DISPLAY PATH HOME LANG USER
fi
#-------------------------------------

# lock
touch /tmp/frontend_startup_lock

. /etc/rc.d/PUPSTATE
[ -z "$PATH" ] && PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
. /etc/eventmanager #has RAMSAVEINTERVAL, HOTPLUGON, HOTPLUGNOISY, BACKENDON, POWERTIMEOUT

######################################################################
#stuff to setup at entry...
mkdir -p /tmp/pup_event_frontend

HOMEDIRS="$HOME"
[ "$HOME" != "/root" ] && HOMEDIRS="$HOME /root"
for i in $HOMEDIRS
do
	if [ ! -L ${i}/.pup_event -a -d ${i}/.pup_event ] ; then
		rm -rf ${i}/.pup_event # a real directory.. destroy
	fi
	ln -sfn /tmp/pup_event_frontend ${i}/.pup_event
done
#--

#build the desktop icons...
PROBEDISK="`probedisk -plus-size`"

if [ -f /usr/local/pup_event/frontend_rox_funcs ] ; then
	. /usr/local/pup_event/frontend_rox_funcs
	# $SHOW_SPLASH is exported by apps...
	if [ $SHOW_SPLASH ] ; then
		/usr/lib/gtkdialog/box_splash -close never -text "$(printf "$(gettext 'Creating desktop icons... please wait...')")" &
		ZPID=$!
	fi
else
	HOTPLUGON='true'
	ICONPARTITIONS='false'
	create_icon_func() {
		local dev=$1
		[ "$DRV_NAME" ] && dev=$DRV_NAME
		if [ ! -d /tmp/pup_event_frontend/drive_${dev} ] ; then
			mkdir -p /tmp/pup_event_frontend/drive_${dev}
		fi
		cat > /tmp/pup_event_frontend/drive_${dev}/AppRun <<EOF
#!/bin/sh
exec /usr/local/pup_event/drive_all $dev
EOF
		chmod +x /tmp/pup_event_frontend/drive_${dev}/AppRun
	}
fi

[ "$ICONDESK" = "false" ] && "$ICONPARTITIONS" = "false"

#-- only show a single 'drives' icon on desktop...
if [ "$HOTPLUGON" = "false" -o "$ICONDESK" = "false" ];then
	DRV_CATEGORY='drive'
	create_icon_func #needs DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION.
#-- PARTITIONS - $PROBEPART
elif [ "$ICONPARTITIONS" = "true" ] ; then
	PROBEPART="`probepart -k -extra-info`"
	while IFS="|" read ONEDRV DRV_CATEGORY DRV_DESCRIPTION DRV_SIZE zz
	do
		DRV_NAME=${ONEDRV##*/}
		echo "$PROBEPART" | grep -q "$DRV_NAME" || {
			continue #precaution (such as CD not inserted).
		}
		create_icon_func startup #needs DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION, PROBEPART.
	done <<< "$PROBEDISK"
#-- DRIVES ONLY - $PROBEDISK
elif [ "$ICONDESK"  = "true" ] ; then #show only DRV icons
	while IFS="|" read ONEDRV DRV_CATEGORY DRV_DESCRIPTION DRV_SIZE zz
	do
		DRV_NAME=${ONEDRV##*/}
		create_icon_func startup #needs DRV_NAME, DRV_CATEGORY, DRV_DESCRIPTION, PROBEPART.
	done <<< "$PROBEDISK"
fi
[ "$ZPID" ] && kill $ZPID
######################################################################

# unlock
rm -f /tmp/frontend_startup_lock

if [ "$BACKENDON" = "false" ];then #see /etc/eventmanager.
	killall frontend_timeout 2>/dev/null
	killall udevd 2>/dev/null
	exit 2
else
	. /etc/rc.d/BOOTCONSTRAINED
	[ "$BOOT_UDEVDCHILDREN" ] && UOPT="--children-max=${BOOT_UDEVDCHILDREN}"
	if ! pidof udevd &>/dev/null ; then
		UDEV_LOG=2 udevd --daemon --resolve-names=early ${UOPT} &
	fi
fi

exit 0

### END ###